<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category StockController.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/5/28  17:27
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Stock;
use App\Services\StockService;
use Carbon\Carbon;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;

class StockController extends Controller
{
    /**
     * @var string
     */
    protected $_model = 'stock';

    /**
     * @var
     */
    protected $stock_list;

    /**
     * @var Repository|mixed
     */
    protected $get_url;

    /**
     * @var StockService
     */
    protected $stockService;

    /**
     * @var float
     */
    protected $ecpect_level = 0.02;

    public function __construct()
    {
        $this->get_url      = config('other.stock_url');
        $this->stockService = StockService::getInstance();
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:47
     */
    public function index(Request $request)
    {
        $compact = compact('request');
        return view('admin.' . $this->_model . '.index', $compact);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/4/21  20:29
     */
    public function getList(Request $request)
    {
        $responseData = $this->stockService->getList($request);
        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return Application|Factory|View
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/4/21  20:29
     */
    public function create(Request $request)
    {
        return view('admin.stock.create', compact('request'));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/3/8  17:19
     */
    public function store(Request $request): JsonResponse
    {
        $responseData = $this->stockService->addData($request, $this->get_url);
        return response()->json($responseData);

    }

    public function edit(Request $request, $id)
    {
        $data = $this->stockService->getOne($id);
        return view('admin.stock.edit', compact('request', 'data'));

    }

    public function update(Request $request, $id)
    {
        $responseData = $this->stockService->editData($request, $id);

        return response()->json($responseData);
    }

    public function destroy(Request $request)
    {
        $ids          = $request->id;
        $responseData = $this->stockService->delData($ids);

        return response()->json($responseData);
    }

    public function restCost(Request $request)
    {
        $responseData = $this->stockService->setCost($request, $this->get_url);
        return response()->json($responseData);

    }

    public function setAttention(Request $request)
    {
        $responseData = $this->stockService->setAttention($request->id);

        return response()->json($responseData);
    }

    public function setWarm(Request $request)
    {
        $responseData = $this->stockService->setWarm($request->id);

        return response()->json($responseData);
    }


    public function isInit(Request $request)
    {
        $responseData = $this->stockService->isInit($request->id);

        return response()->json($responseData);

    }

    public function initStockList()
    {
        $responseData = $this->stockService->stockYnData(1);

        return response()->json($responseData);

    }

}
